<template> 
	<div class="JNPF-common-layout">
		<div class="JNPF-common-layout-center">
			<el-row class="JNPF-common-search-box" :gutter="16">
				<el-form @submit.native.prevent>
					<el-col :span="6">
						<el-form-item label="设备编码">
							<el-input v-model="query.sbbm" placeholder="设备编码" clearable  />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="设备类型">
							<el-select v-model="query.sblx" placeholder="设备类型" >
								<el-option v-for="(item, index) in sblxOptions" :key="index" :label="item.fullName" :value="item.id"  />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报警状态">
							<el-select v-model="query.bjzt" placeholder="报警状态" >
								<el-option v-for="(item, index) in bjztOptions" :key="index" :label="item.fullName" :value="item.id"  />
							</el-select>
						</el-form-item>
					</el-col>
		<template v-if="showAll">
					<el-col :span="6">
						<el-form-item label="是否连锁">
							<el-select v-model="query.sflsbj" placeholder="是否连锁" >
								<el-option v-for="(item, index) in sflsbjOptions" :key="index" :label="item.fullName" :value="item.id"  />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="所属危险源名称">
							<el-select v-model="query.sszdwxydymc" placeholder="所属危险源名称" clearable >
								<el-option v-for="(item, index) in sszdwxydymcOptions" :key="index" :label="item.ZDWXYMC" :value="item.ID"  />
							</el-select>
						</el-form-item>
					</el-col>
		</template>
					<el-col :span="6">
					<el-form-item>
						<el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
						<el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
						<el-button type="text" icon="el-icon-arrow-down" @click="showAll=true" v-if="!showAll">展开</el-button>
						<el-button type="text" icon="el-icon-arrow-up" @click="showAll=false" v-else>收起</el-button>
					</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<div class="JNPF-common-layout-main JNPF-flex-main">
				<div class="JNPF-common-head">
					<div>
						<!-- <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">
							新增原来
						</el-button> -->
                        <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandleNew('','add')">
							登记
						</el-button>
						<el-button type="text" icon="el-icon-download" @click="exportData()">
							导出
						</el-button>
					</div>
					<div class="JNPF-common-head-right">
						<el-tooltip effect="dark" content="刷新" placement="top">
							<el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false"  @click="reset()" />
						</el-tooltip>
						<screenfull :isContainer="true" />
						<el-tooltip effect="dark" content="列设置" placement="top">
							<el-link icon="icon-ym icon-ym-options JNPF-common-head-icon" :underline="false" />
						</el-tooltip>
					</div>
				</div>
				<JNPF-table v-loading="listLoading" :data="list">
					<el-table-column prop="sbbm" label="设备编码" align="left" />
					<el-table-column label="所属危险源名称"  prop="sszdwxydymc" algin="left">
						<template slot-scope="scope">{{ scope.row.sszdwxydymc | dynamicText(sszdwxydymcOptions) }}</template>
					</el-table-column>
					<el-table-column label="设备类型"  prop="sblx" algin="left">
						<template slot-scope="scope">{{ scope.row.sblx | dynamicText(sblxOptions) }}</template>
					</el-table-column>
					<el-table-column label="运行状态"  prop="sbyxzt" algin="left">
						<template slot-scope="scope">{{ scope.row.sbyxzt | dynamicText(sbyxztOptions) }}</template>
					</el-table-column>
					<el-table-column label="采集类型"  prop="cjlx" algin="left">
						<template slot-scope="scope">{{ scope.row.cjlx | dynamicText(cjlxOptions) }}</template>
					</el-table-column>
					<el-table-column prop="cjsz" label="采集数值" align="left" />
					<el-table-column label="是否连锁"  prop="sflsbj" algin="left">
						<template slot-scope="scope">{{ scope.row.sflsbj | dynamicText(sflsbjOptions) }}</template>
					</el-table-column>
					<el-table-column label="报警状态"  prop="bjzt" algin="left">
						<template slot-scope="scope">
                            <!-- {{ scope.row.bjzt | dynamicText(bjztOptions) }} -->
                            <span v-if="scope.row.bjzt==0" style="color:red !important;">报警</span>
                            <span v-if="scope.row.bjzt==1" style="color:green !important;">正常</span>
                        </template>
					</el-table-column>
					<el-table-column prop="bjms" label="报警描述" align="left" />
					<el-table-column prop="jcrq" label="采集日期" align="left" />
					<el-table-column label="操作" fixed="right" width="250">
						<template slot-scope="scope">
							<!-- <el-button type="text" @click="addOrUpdateHandle(scope.row.id)" >
								编辑111
							</el-button>
							<el-button type="text" @click="handleDel(scope.row.id)" class="JNPF-table-delBtn">
								删除111
							</el-button> -->
                            <el-button type="text" @click="addOrUpdateHandleNew(scope.row.id,'updata')" >
								变更
							</el-button>
							<el-button type="text" @click="addOrUpdateHandleNew(scope.row.id,'del')" class="JNPF-table-delBtn">
								删除
							</el-button>
							<el-button type="text" @click="addOrUpdateHandle(scope.row.id,true)" >
								详情
							</el-button>
                            <el-button type="text" @click="historyData(scope.row)" >
								历史数据
							</el-button>
						</template>
					</el-table-column>
				</JNPF-table>
				<pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
			</div>
		</div>
		<JNPF-Form v-if="formVisible" ref="JNPFForm" @refresh="refresh" /> 
		<ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />

        <flow v-show="flowVisible1" ref="flow" @close="flowVisible1=false" @chioceFlow="chioceFlow" />
        <hisTable v-if="flowhisTable" ref="hisTable" @close="flowhisTable=false" @refresh="refresh" />
        <hisEchart v-if="flowhisEchart" ref="hisEchart" @close="flowhisEchart=false" @refresh="refresh" />

        <!-- <exportbox v-if="exportBoxVisible" ref="ExportBox" @download="download" />  -->
        <component :is="currentView" v-if="formVisible1" @close="colseForm" ref="JNPFForm1" />   

        
	</div>

</template> 
<script>
    import request from '@/utils/request'
    import {getDictionaryDataSelector} from '@/api/systemData/dictionary'
    import JNPFForm from './Form'  // 详情
    import ExportBox from './ExportBox' // 导出
    import {previewDataInterface} from '@/api/systemData/dataInterface'
    import { FlowEngineListAll } from '@/api/workFlow/FlowEngine'
    import edit from '@/views/workFlow/fromBox/Edit'
    import audit from '@/views/workFlow/fromBox/Audit'
    import flow from './Flow'
    
    import hisTable from './HisTable' // 历史数据
    import hisEchart from './HisEchart' // 历史数据

    export default {
        // components: {JNPFForm, ExportBox,edit, audit, flow, ExportBox},
        components: {JNPFForm, ExportBox,edit, audit, flow, ExportBox, hisTable, hisEchart},
        data() {
            return {
                showAll: false,
                query: {
					sbbm:undefined,
					sblx:undefined,
					bjzt:undefined,
					sflsbj:undefined,
					sszdwxydymc:undefined,

                },
                defaultProps: {
                    children: 'children',
                    label: 'fullName'
                },
                list: [],
                listLoading: true,
                total: 0,
                listQuery: {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                },
                formVisible: false,
                exportBoxVisible: false,
                columnList: [
					{prop: 'sbbm', label: '设备编码'},
					{prop: 'sszdwxydymc', label: '所属危险源名称'},
					{prop: 'sblx', label: '设备类型'},
					{prop: 'sbyxzt', label: '运行状态'},
					{prop: 'cjlx', label: '采集类型'},
					{prop: 'cjsz', label: '采集数值'},
					{prop: 'sflsbj', label: '是否连锁'},
					{prop: 'bjzt', label: '报警状态'},
					{prop: 'bjms', label: '报警描述'},
					{prop: 'jcrq', label: '采集日期'},

                ],
				sszdwxydymcOptions:[],
				sblxOptions:[{"fullName":"温度","id":"0"},{"fullName":"压力","id":"1"},{"fullName":"液位","id":"2"},{"fullName":"流量","id":"3"},{"fullName":"ESD按钮","id":"4"},{"fullName":"气体","id":"5"},{"fullName":"其他","id":"6"}],
				sbyxztOptions:[{"fullName":"停用","id":"0"},{"fullName":"在用","id":"1"}],
				sfhxcsOptions:[{"fullName":"否","id":"0"},{"fullName":"是","id":"1"}],
				scdylxOptions:[],
				aqjbOptions:[{"fullName":"一级","id":"0"},{"fullName":"二级","id":"1"},{"fullName":"三级","id":"2"}],
				positionOptions:[{"fullName":"一级","id":"0"},{"fullName":"二级","id":"1"},{"fullName":"三级","id":"2"}],
				cjlxOptions:[{"fullName":"数字量","id":"0"},{"fullName":"模拟量","id":"1"}],
				sflsbjOptions:[{"fullName":"否","id":"0"},{"fullName":"是","id":"1"}],
				bjztOptions:[{"fullName":"报警","id":"0"},{"fullName":"正常","id":"1"}],
				sydwOptions:[],

                categoryList: [],
                flowEngineListAll: [],
                formVisible1: false,
                flowVisible1: false,
                currentView: 'edit',

                //-
                flowhisTable: false,
                flowhisEchart: false

            }
        },
        computed: {},
        created() {
			this.initData();

            this.getSszdwxydymcOptions();
            this.getScdylxOptions();
            this.getSydwOptions();

            this.getDictionaryData();

        },
        methods: {
            getSszdwxydymcOptions()
            {
                previewDataInterface('707f1102bce74818a551ebcfa88f2321').then(res => {
                    this.sszdwxydymcOptions = res.data
                })
            },
            getScdylxOptions()
            {
                previewDataInterface('e7b2c31f5a7147c08629c5409dd7fdf4').then(res => {
                    this.scdylxOptions = res.data
                })
            },
            getSydwOptions()
            {
                previewDataInterface('772d2cbcadd24df79243e3d92ef5f690').then(res => {
                    this.sydwOptions = res.data
                })
            },


            initData() {
                this.listLoading = true;
                let query = {
                    ...this.listQuery,
                    ...this.query
                };
                request({
                    url: `/api/System/JCZBYJ`,
                    method: 'get',
                    data: query
                }).then(res => {
                    this.list = res.data.list
                    this.total = res.data.pagination.total
                    this.listLoading = false
                    this.$nextTick(() => {
                        this.tableHeight = "100%"
                    });
                })
            },
            handleDel(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    request({
                        url: `/api/System/JCZBYJ/${id}`,
                        method: 'DELETE'
                    }).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.msg,
                            onClose: () => {
                                this.initData()
                            }
                        });
                    })
                }).catch(() => {
                });
            },
            addOrUpdateHandle(id,isDetail) {
                this.formVisible = true
                this.$nextTick(() => {
                    this.$refs.JNPFForm.init(id,isDetail)
                })
            },
            exportData() {
                this.exportBoxVisible = true
                this.$nextTick(() => {
                    this.$refs.ExportBox.init(this.columnList)
                })
            },
            download(data) {
                let query = {...data, ...this.listQuery, ...this.query}
                request({
                    url: `/api/System/JCZBYJ/Actions/Export`,
                    method: 'POST',
                    data: query
                }).then(res => {
                    if (!res.data.url) return
                    window.location.href = this.define.comUrl + res.data.url
                    this.$refs.ExportBox.visible = false
                    this.exportBoxVisible = false
                })
            },
            search() {
                this.listQuery = {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                }
                this.initData()
            },
            refresh(isrRefresh) {
                this.formVisible = false
                if (isrRefresh) this.reset()
            },
            reset() {
                for (let key in this.query) {
                    this.query[key] = undefined
                }
                this.listQuery = {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                }
                this.initData()
            },



            //-
            addOrUpdateHandleNew(id,isDetail){
                this.flowVisible1 = true
                this.$nextTick(() => {
                    let flowAllList = this.flowEngineListAll
                    if(flowAllList.length > 0){
                        for(let i=0;i<flowAllList.length;i++){
                            if(flowAllList[i].fullName == "危险源预警管理"){
                                if(flowAllList[i].children.length > 0){
                                    let childList = flowAllList[i].children;
                                    for(let r=0;r<childList.length;r++){
                                        if(isDetail == 'add'){
                                            if(childList[r].id == "396bbfee7a074fd98fa0e3181f423251"){  //新增1
                                                this.$refs.flow.init1(this.flowEngineListAll,'add')
                                            }
                                        }else if(isDetail == 'updata'){
                                            if(childList[r].id == "e5809a3d56b144ef9a67f7d120a18979"){  //编辑1(变更)
                                                this.$refs.flow.init1(this.flowEngineListAll,'updata',id)
                                            }
                                        }else if(isDetail == 'del'){
                                            if(childList[r].id == "b179dbc25b42446a8a7f239c7e9d42c2"){  //删除1
                                                this.$refs.flow.init1(this.flowEngineListAll,'del',id)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                })
            },
            getFlowEngineList() {
                FlowEngineListAll().then((res) => {
                    this.flowEngineList = res.data.list
                    this.flowEngineListAll = JSON.parse(JSON.stringify(this.categoryList))
                    for (let i = 0; i < this.flowEngineListAll.length; i++) {
                        let child = this.flowEngineList.filter(o => this.flowEngineListAll[i].enCode === o.category)
                        let count = child.length
                        this.$set(this.flowEngineListAll[i], 'children', child)
                        this.$set(this.flowEngineListAll[i], 'count', count)
                    }
                    this.flowEngineListAll = this.flowEngineListAll.filter(o => o.children.length)
                })
            },
            getDictionaryData() {
                this.$store.dispatch('base/getDictionaryData', { sort: 'WorkFlowCategory' }).then((res) => {
                    this.categoryList = res
                    this.getFlowEngineList()
                })
            },

            chioceFlow(item,mainId) {
                let data = {
                    enCode: item.enCode,
                    flowId: item.id,
                    formType: item.formType,
                    id: '',
                    mainid: mainId
                }
                this.currentView = 'edit'
                this.$nextTick(() => {
                    this.formVisible1 = true
                    this.$nextTick(() => {
                        this.$refs.JNPFForm1.init(data)
                        this.flowVisible1 = false
                    })
                })
            },
            toDetail(item) {
                let data = {
                    enCode: item.flowCode,
                    flowId: item.flowId,
                    delegateId: item.delegateId,
                    id: item.id,
                    formType: item.formType,
                    isSelf: true,
                    status: item.status,
                    readonly: true,
                    showStatus: true
                }
                this.currentView = 'audit'
                this.$nextTick(() => {
                    this.formVisible1 = true
                    this.$nextTick(() => {
                    this.$refs.JNPFForm1.init(data)
                    })
                })
            },
            colseForm(isRefresh) {
                this.formVisible1 = false
                if (isRefresh) this.refresh()
            },


            //-
            historyData(row){
                request({
                    url: '/api/System/JCZBYJ/' + row.id,
                        method: 'get'
                    }).then(res =>{
                    // this.dataForm = res.data;
                    if(row.cjlx == '0'){
                        this.flowhisTable = true
                        this.$nextTick(() => {
                            this.$refs.hisTable.initTable(res.data)
                        }) 
                    }else if(row.cjlx == '1'){
                        this.flowhisEchart = true
                        this.$nextTick(() => {
                            this.$refs.hisEchart.init(res.data)
                        }) 
                    }
                })
            }

        }
    }
</script>